package us.icitap.commandcenter.ui.configuration;

import java.util.List;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.Result;
import us.icitap.commandcenter.business.ejb.persistence.CallStatus;
import us.icitap.commandcenter.ui.custom.CustSaveButton;
import us.icitap.commandcenter.ui.custom.CustTextField;
import us.icitap.commandcenter.ui.custom.CustVerticalLayout;
import us.icitap.commandcenter.ui.custom.StatusColumnGenerator;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.Action;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class CallStatusLayout extends CustVerticalLayout implements ClickListener{

	private final CustTextField callStatusTextField = new CustTextField("Status per telefonaten");
	private final CustSaveButton addButton = new CustSaveButton("SHTO");
	
	
	final BeanItemContainer<CallStatus> beans = new BeanItemContainer<CallStatus>(CallStatus.class);
	final Table table = new Table(null,beans);
	
	static final Action ACTION_ACTIVE = new Action("Bej AKTIVE");
    static final Action ACTION_UNACTIVE = new Action("Bej JO-AKTIVE");
    
    public CallStatusLayout() {
    	this.setCaption("Statuset per telefonaten");
		this.addStyleName("borders");
		
		final FormLayout fl = new FormLayout(callStatusTextField);
		final HorizontalLayout hl = new HorizontalLayout(fl,addButton);
		hl.setComponentAlignment(addButton, Alignment.MIDDLE_RIGHT);
		this.addComponent(hl);

		table.setVisibleColumns(new Object[]{"id","tag", "status"});
		table.setColumnWidth("tag", 400);
		table.setColumnWidth("status", 100);

		table.setPageLength(8);
		
		table.addGeneratedColumn("status", new StatusColumnGenerator());
		this.addComponent(table);
		
		
		table.addActionHandler(new Action.Handler() {
            public Action[] getActions(Object target, Object sender) {
               if(target == null) return null;
               CallStatus cs = (CallStatus)target;
               if(cs.isStatus())
            	   return new Action[]{ACTION_UNACTIVE};
               else
            	   return new Action[]{ACTION_ACTIVE};
            }
            
            public void handleAction(Action action, Object sender, Object target) {
            	 CallStatus cs = (CallStatus)target;
            	if (ACTION_ACTIVE == action) {
                    cs.setStatus(true);
                    Result<CallStatus> result  = CommandCenterUI.persistenceBean.saveEntity(cs);
                    if(result.isSucceeded())
                    	table.refreshRowCache();
                    else
                    	System.out.println(result.getMessage());
                }
                else if (ACTION_UNACTIVE == action) {
                    cs.setStatus(false);
                    Result<CallStatus> result  = CommandCenterUI.persistenceBean.saveEntity(cs);
                    if(result.isSucceeded())
                    	table.refreshRowCache();
                    else
                    	System.out.println(result.getMessage());
                }

        }});

		this.addButton.addClickListener(this);
		
		this.refreshTable();
	}
    
    
    private void refreshTable(){
		Result<List<CallStatus>> result =  CommandCenterUI.persistenceBean.getList(CallStatus.class, false, null);
		
		if(result.isSucceeded()){
			beans.removeAllItems();
			beans.addAll(result.getObject());
		}
	}
    
    @Override
	public void buttonClick(ClickEvent event) {
		try {
			if(this.callStatusTextField.getValue() != null && !"".equals(this.callStatusTextField.getValue().toString().trim())){
				CallStatus cs = new CallStatus();
				cs.setTag(this.callStatusTextField.getValue().toString());
				Result<CallStatus> result = CommandCenterUI.persistenceBean.saveEntity(cs);
				if(result.isSucceeded()){
					System.out.println("Ruajtja OK");
					this.refreshTable();
				}
				else
					System.out.println(result.getMessage());
				
			}
			else
				System.out.println("BOSH");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}
